Skip to content

Fix subclass discovery to support multi-level parser hierarchies - #22

Open
awe-srush wants to merge 1 commit into
google:mainfrom
awe-srush:bug/fix-subclass-discovery
Open

Fix subclass discovery to support multi-level parser hierarchies#22
awe-srush wants to merge 1 commit into
google:mainfrom
awe-srush:bug/fix-subclass-discovery

Conversation

@awe-srush

Copy link
Copy Markdown

Summary

Fixes a bug in get_parser_class() where parsers registered two or more levels deep in the AbstractLanguageParser hierarchy were silently ignored, causing Vanir to fail to detect a matching parser even when one was available.

Problem

get_parser_class() used AbstractLanguageParser.__subclasses__() to discover registered parsers. Python's __subclasses__() only returns direct subclasses, it does not recurse into grandchildren or deeper.

The existing C++ and Java parsers subclass AbstractLanguageParser directly so this was never an issue. However, tree-sitter-backed parsers introduce an intermediate base class:

AbstractLanguageParser
    └── TreeSitterParserBase    (intermediate base, not a concrete parser)
            └── PythonParser    (concrete parser, was invisible to subclasses())

PythonParser would never be returned by __subclasses__(), so any .py file passed to get_parser_class() would return None and raise NotImplementedError.

Fix

Replaces the single __subclasses__() call with a recursive helper _all_subclasses(cls) that walks the full subclass tree and returns only concrete (non-abstract) classes:

def _all_subclasses(cls):
    result = []
    for sub in cls.__subclasses__():
        if not sub.__abstractmethods__:
            result.append(sub)
        result.extend(_all_subclasses(sub))
    return result
  • Abstract intermediate classes (e.g. TreeSitterParserBase) are excluded via the __abstractmethods__ check
  • All concrete leaf parsers at any depth are included
  • Existing C++ and Java parser behaviour is completely unchanged

Dependencies

This PR is independent and can be merged in any order alongside other PRs in this series. It must be merged before feat/python-parser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant